package cn.facetoken.homer;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import java.util.Arrays;

class PermissionHelper implements DialogInterface.OnClickListener {

    public PermissionHelper(@NonNull final Activity activity) {
        this.activity = activity;
    }

    @NonNull
    private final Activity activity;

    public void setupPermission() {
        Log.i(getClass().getSimpleName(), "setupPermission(): Checking permission to read/write external storage and camera...");
        if (activity.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || activity.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            Log.w(getClass().getSimpleName(), "setupPermission(): Permission to read/write external storage or camera was denied. Continue to request the permission.");
            if (activity.shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE) || activity.shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE) || activity.shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                Log.i(getClass().getSimpleName(), "setupPermission(): Should show request permission rationale to read/write external storage or camera. Finished.");
                new AlertDialog.Builder(activity).setTitle(R.string.app_name).setMessage(R.string.permission_dialog_message).setPositiveButton(android.R.string.ok, this).create().show();
            } else {
                Log.i(getClass().getSimpleName(), "setupPermission(): Requesting permission to read/write external storage and camera...");
                activity.requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, DemonstrationActivity.REQUEST_CODE_STORAGE);
            }
        } else {
            Log.i(getClass().getSimpleName(), "setupPermission(): Permission to read external storage was granted.");
        }
    }

    public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
        Log.d(getClass().getSimpleName(), String.format("onRequestPermissionsResult(): Request Code = %d, Granted Permissions = %s, Grant Result = %s.", requestCode, Arrays.toString(permissions), Arrays.toString(grantResults)));

        if (grantResults.length != permissions.length) {
            new AlertDialog.Builder(activity).setTitle(R.string.app_name).setMessage(R.string.grant_permission_error_dialog_message).setPositiveButton(android.R.string.ok, this).create().show();
            return;
        }

        for (int i = 0; i < grantResults.length; i++) {
            if (PackageManager.PERMISSION_GRANTED == grantResults[i]) {
                Log.d(getClass().getSimpleName(), String.format("onRequestPermissionsResult(): Permission %s was granted.", permissions[i]));
            } else {
                Log.e(getClass().getSimpleName(), String.format("onRequestPermissionsResult(): Permission %s was denied.", permissions[i]));
                new AlertDialog.Builder(activity).setTitle(R.string.app_name).setMessage(R.string.grant_permission_error_dialog_message).setPositiveButton(android.R.string.ok, this).create().show();
            }
        }
    }

    @Override
    public void onClick(@NonNull final DialogInterface dialog, final int which) {
        Log.d(getClass().getSimpleName(), "onClick(): Dialog was clicked. Finish.");
        activity.finish();
    }
}